package com.darchrow.nio.file;

import com.alibaba.fastjson.JSON;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 传统IO VS NIO
 *
 * @author mdl
 * @date 2020/5/6 13:20
 */
public class SimpleFileTransferTest {

    private long transferFile(File source, File des) throws IOException {
        long startTime = System.currentTimeMillis();
        if (!des.exists()) {
            des.createNewFile();
        }
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(des));
        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }
        long endTime = System.currentTimeMillis();
        return endTime - startTime;
    }

    private long transferFileWithNIO(File source, File des) throws IOException {
        long startTime = System.currentTimeMillis();
        if (!des.exists()) {
            des.createNewFile();
        }
        RandomAccessFile read = new RandomAccessFile(source, "rw");
        RandomAccessFile write = new RandomAccessFile(des, "rw");

        FileChannel readChannel = read.getChannel();
        FileChannel writeChannel = write.getChannel();

        // 1M缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1024);
        while (readChannel.read(byteBuffer) > 0) {
            byteBuffer.flip();
            writeChannel.write(byteBuffer);
            byteBuffer.clear();
        }
        writeChannel.close();
        readChannel.close();
        long endTime = System.currentTimeMillis();
        return endTime - startTime;
    }

    public static void main(String[] args) throws IOException {
//        SimpleFileTransferTest simpleFileTransferTest = new SimpleFileTransferTest();
//        // 364M
//        File source = new File("E:\\NIO\\mysql8.tar.xz");
//        File des = new File("E:\\NIO\\temp.tar.xz");
//
//
//        long useTime = simpleFileTransferTest.transferFile(source, des);
//        System.out.println("364M..传统IO用时: " + useTime);
//        useTime = simpleFileTransferTest.transferFileWithNIO(source, des);
//        System.out.println("364M..NIO用时: " + useTime);
//
//
//        System.out.println("-----------------------------------------------");
//        // 220M
//        source = new File("E:\\NIO\\深入理解Java虚拟机.pdf");
//        des = new File("E:\\NIO\\temp2.pdf");
//
//        useTime = simpleFileTransferTest.transferFile(source, des);
//        System.out.println("220M..传统IO用时: " + useTime);
//        useTime = simpleFileTransferTest.transferFileWithNIO(source, des);
//        System.out.println("220M..NIO用时: " + useTime);
//
//
//        System.out.println("-----------------------------------------------");
//
//        // 80M
//        source = new File("E:\\NIO\\从Paxos到Zookeeper分布式一致性原理与实践.pdf");
//        des = new File("E:\\NIO\\temp3.pdf");
//
//        useTime = simpleFileTransferTest.transferFile(source, des);
//        System.out.println("80M..传统IO用时: " + useTime);
//        useTime = simpleFileTransferTest.transferFileWithNIO(source, des);
//        System.out.println("80M..NIO用时: " + useTime);


//        Map<Integer, Integer> integerListHashMap = new HashMap<>();
//        integerListHashMap.put(1, 0);
//        integerListHashMap.put(2, 1);
//
//        System.out.println(JSON.toJSONString(integerListHashMap));

        int i=3;

        System.out.println(i/2 + i % 2);



    }

}
